package com.converter;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.EnumSet;
import java.util.Iterator;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

/**
 * @author Ankit
 *
 */
public class CurrencyConverter extends JFrame {
	

	private JPanel contentPane;
	private JTextField ammount;
	private JLabel resultScreen;
	private JComboBox convert;
	private JEditorPane editorPane;
	private JComboBox convertTo;
	private static CurrencyConverter currencyConverter=null;
	private String html;
	private JTextField amount2;
	private JButton btnConvertButton;
	private JLabel result2;	
	private JPanel panel_1;

	/**
	 * @return the result2
	 */
	public JLabel getResult2() {
		return result2;
	}

	/**
	 * @param result2 the result2 to set
	 */
	public void setResult2(JLabel result2) {
		this.result2 = result2;
	}

	public JTextField getAmount2() {
		return amount2;
	}

	public void setAmount2(JTextField amount2) {
		this.amount2 = amount2;
	}

	public JComboBox getConvert() {
		return convert;
	}

	public void setConvert(JComboBox convert) {
		this.convert = convert;
	}

	public JComboBox getConvertTo() {
		return convertTo;
	}

	public void setConvertTo(JComboBox convertTo) {
		this.convertTo = convertTo;
	}

	public JLabel getResultScreen() {
		return resultScreen;
	}

	public void setResultScreen(JLabel resultScreen) {
		this.resultScreen = resultScreen;
	}
	public static CurrencyConverter getCurruncyConverter(){
		if(currencyConverter == null)
			currencyConverter=new CurrencyConverter();
		return currencyConverter;
	}

	/**
	 * Launch the application.
	 */
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					String html;
					CurrencyConverter frame = new CurrencyConverter();
					getCurruncyConverter();
					 html="<html><head><title>Simple Page</title></head>";
					 html+="<body bgcolor='#777779'><hr/><font size=50>This is Html content</font><hr/>";
					 html+="</body></html>";
				//	 URL u=new URL("http://google.com");
				// JEditorPane ed1=new JEditorPane("text/html",html);
				//	 JEditorPane ed1=new JEditorPane(u);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public CurrencyConverter() {
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 604, 300);
		URL test = CurrencyConverter.class.getResource("/CurruncyConverter.png");
        System.out.println(test);
		setIconImage(Toolkit.getDefaultToolkit().getImage(CurrencyConverter.class.getResource("/CurruncyConverter.png")));
		
		setTitle("Currency Converter");
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(null);
		contentPane.setBackground(Color.WHITE);
		setContentPane(contentPane);
		
		resultScreen = new JLabel("");
		resultScreen.setForeground(Color.LIGHT_GRAY);
		resultScreen.setFont(new Font("Times New Roman", Font.BOLD, 15));
		resultScreen.setBounds(23, 30, 216, 21);
		resultScreen.setVisible(false);
		//resultScreen.setFont(getFont().)
		contentPane.add(resultScreen);
		
		btnConvertButton = new JButton("Convert");
		btnConvertButton.setBackground(Color.WHITE);
		btnConvertButton.setIcon(new ImageIcon(CurrencyConverter.class.getResource("/convert.png")));
		btnConvertButton.setBorder(null);
		btnConvertButton.addKeyListener(new KeyListener() {
			
			@Override
			public void keyTyped(KeyEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void keyReleased(KeyEvent arg0) {
				btnConvertButton.setIcon(new ImageIcon(CurrencyConverter.class.getResource("/convert.png")));
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void keyPressed(KeyEvent arg0) {
				btnConvertButton.setIcon(new ImageIcon(CurrencyConverter.class.getResource("/convert_Pressed.png")));
				// TODO Auto-generated method stub
				
			}
		});
		btnConvertButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Converter converter = new Converter();
				String countryToName = toCountry();
				String countryFromName = fromCountry();
				String resultFromTo = ammount.getText();
				String resultFromFrom = amount2.getText();
				String display1 = resultFromTo+" "+countryToName+" equal";
				String display2= resultFromFrom+" "+countryFromName+"";
				getResultScreen().setText(display1);
				resultScreen.setVisible(true);
				getResult2().setText(display2);
				result2.setVisible(true);
			}
		});
		
		
		btnConvertButton.setBounds(132, 192, 62, 59);
		contentPane.add(btnConvertButton);
		
		
		ammount = new JTextField();
		ammount.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent arg0) {
				
				Converter converter = new Converter();
				String contoryTo = to();
				String contryFrom = from();
				
				if(ammount.getText().isEmpty()){
					amount2.setText("");
				}
				if(!ammount.getText().isEmpty()){
				double val = Integer.parseInt(ammount.getText());
				DecimalFormat df = new DecimalFormat("#.##");
				double result = converter.getConvertedValue(val, contoryTo,contryFrom);
				String res = df.format(result);
				amount2.setText(res);
				}
			}
		});
		ammount.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(java.awt.event.MouseEvent evt) {
			
			}
		});
		ammount.setBounds(42, 113, 106, 21);
		contentPane.add(ammount);
		ammount.setColumns(10);
		
		convert = new JComboBox();
		convert.setBorder(null);
		Iterator<String> itr = Currency.getCurrencylookup().values().iterator();
		while(itr.hasNext()){
			String country = itr.next();
			convert.addItem(country);
		}
		convert.setBackground(Color.WHITE);
		convert.setForeground(Color.BLACK);
		
		convert.setBounds(163, 112, 151, 22);
		contentPane.add(convert);
		
		convertTo = new JComboBox();
		Iterator<String> iterator = Currency.getCurrencylookup().values().iterator();
		while(iterator.hasNext()){
			String country = iterator.next();
			convertTo.addItem(country);
		}
		convertTo.setBackground(Color.WHITE);
		convertTo.setForeground(Color.BLACK);
		convertTo.setBounds(163, 146, 151, 22);
		contentPane.add(convertTo);
		
		String html;
		html="<script async src=\"//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js\"></script>";
		html+="<!-- app -->";
		html+="<ins class=\"adsbygoogle\"";
		html+="style=\"display:inline-block;width:300px;height:250px\"";
		html+="data-ad-client=\"ca-pub-6608086865031333\"";
		html+="data-ad-slot=\"6753176672\"></ins>";
		html+="<script>";
		html+="(adsbygoogle = window.adsbygoogle || []).push({});";
		html+="</script>";

		
				editorPane = new JEditorPane("text/html",html);
				editorPane.setBounds(396, 11, 142, 240);
				editorPane.setVisible(true);
				contentPane.add(editorPane);
				
				amount2 = new JTextField();
				amount2.addKeyListener(new KeyAdapter() {
					@Override
					public void keyReleased(KeyEvent arg0) {
						
						if(amount2.getText().isEmpty()){
							ammount.setText("");
						}
						if(!amount2.getText().isEmpty()){
							Converter converter = new Converter();
							String contoryTo = to();
							String contryFrom = from();
							double val = Integer.parseInt(amount2.getText());
							DecimalFormat df = new DecimalFormat("#.##");
						double result = converter.getConvertedValue(val, contryFrom,contoryTo);
						String res = df.format(result);
						ammount.setText(res);	
						}
						}
				});
				amount2.setBounds(41, 149, 107, 21);
				contentPane.add(amount2);
				amount2.setColumns(10);
				
				result2 = new JLabel("New label");
				result2.setForeground(SystemColor.windowText);
				result2.setFont(new Font("Times New Roman", Font.BOLD, 27));
				result2.setBounds(23, 49, 291, 53);
				result2.setVisible(false);
				contentPane.add(result2);
				
				JPanel panel = new JPanel();
				panel.setForeground(Color.WHITE);
				panel.setBorder(null);
				panel.setBackground(new Color(204, 204, 204));
				panel.setBounds(10, 100, 314, 2);
				contentPane.add(panel);
				
				panel_1 = new JPanel();
				panel_1.setForeground(Color.WHITE);
				panel_1.setBorder(null);
				panel_1.setBackground(new Color(204, 204, 204));
				panel_1.setBounds(10, 179, 314, 2);
				contentPane.add(panel_1);
	//	JEditorPane editorPane = new JEditorPane("text/html",html);
		
	}
	
	/**
	 * @return
	 */
	public String to(){
		
			Converter converter = new Converter();
			EnumSet<Currency> enmCurrency = EnumSet.allOf(Currency.class);
				Iterator<Currency> enmItr = enmCurrency.iterator();
				String to = null;
				String convertCountryName = null;
					while(enmItr.hasNext()){
						Currency currency = enmItr.next();
						convertCountryName = (String) convert.getSelectedItem();
						if(currency.getCurrencyCodeFullName().equals(convertCountryName)){
							to = currency.getCurrencyCode();
						}
					
				}
				return to;
			}
	public String from (){
		String convertToCountryName=null;
		String from = null;
			EnumSet<Currency> enmCurrency = EnumSet.allOf(Currency.class);
			Iterator<Currency> enmItr = enmCurrency.iterator();
				while(enmItr.hasNext()){
				Currency currency = enmItr.next();
				convertToCountryName = (String) convertTo.getSelectedItem();
	
				if(currency.getCurrencyCodeFullName().equals(convertToCountryName)){
					from = currency.getCurrencyCode();
					}	
				}
				return from;
		}
	
	public String toCountry(){
		
		Converter converter = new Converter();
		EnumSet<Currency> enmCurrency = EnumSet.allOf(Currency.class);
			Iterator<Currency> enmItr = enmCurrency.iterator();
			String to = null;
			String convertCountryName = null;
				while(enmItr.hasNext()){
					Currency currency = enmItr.next();
					convertCountryName = (String) convert.getSelectedItem();
					if(currency.getCurrencyCodeFullName().equals(convertCountryName)){
						to = currency.name();
					}
				
			}
			return to;
		}
	
	/**
	 * @return
	 */
	public String fromCountry (){
		
		String convertToCountryName=null;
		String from = null;
			EnumSet<Currency> enmCurrency = EnumSet.allOf(Currency.class);
			Iterator<Currency> enmItr = enmCurrency.iterator();
				while(enmItr.hasNext()){
				Currency currency = enmItr.next();
				convertToCountryName = (String) convertTo.getSelectedItem();
	
				if(currency.getCurrencyCodeFullName().equals(convertToCountryName)){
					from = currency.name();
					}	
				}
				return from;
		}
}
